package com.example.startup.manager

import android.content.Context
import android.os.Looper
import com.example.startup.StartUp
import com.example.startup.sort.TopologySort
import java.lang.RuntimeException

class StartUpManager(
     val context: Context,
     val startUpList: ArrayList<StartUp<*>>
) {

    suspend fun start():StartUpManager{
        if (Looper.myLooper() != Looper.getMainLooper()){
            throw RuntimeException("请在主线程中调用startUpManager")
        }
        val sort = TopologySort.sort(startUpList)
        for (item in sort.result){
            item.create(context)
        }
        return this
    }



    class Builder{

        private val startUpList: ArrayList<StartUp<*>> = ArrayList()
        public fun addStartUp(startUp: StartUp<*>): Builder{
            startUpList.add(startUp)
            return this
        }

        public fun builder(context: Context): StartUpManager{
            return StartUpManager(context,startUpList)
        }

    }


}